import { login,getUserInfo } from '../../api/sys/user/user-api';

export default {
    state: {
        loginAccount:localStorage.getItem('setLoginAccount') ? localStorage.getItem('setLoginAccount') : '',
        userName: localStorage.getItem('setUserName') ? localStorage.getItem('setUserName') : '',
        userId: localStorage.getItem('setUserId') ? localStorage.getItem('setUserId') : '',
        avatorImgPath: localStorage.getItem('setAvator') ? localStorage.getItem('setAvator') : '',
        token: localStorage.getItem('setToken') ? localStorage.getItem('setToken') : '',
        access: '',
        msg : '输入任意用户名和密码即可'
    },
    getters: {
        msg(state, getters, rootState){
            return rootState.user.msg;
        },
        token(state, getters, rootState){
            return rootState.user.token;
        }
    },
    mutations: {
        setLoginAccount(state,loginAccount){
            state.loginAccount = loginAccount;
        },
        setAvator(state, avatorPath) {
            state.avatorImgPath = avatorPath;
        },
        setUserId(state, id) {
            state.userId = id;
        },
        setUserName(state, name) {
            state.userName = name;
        },
        setAccess(state, access) {
            state.access = access;
        },
        setToken(state, token) {
            state.token = token;
        },
        setMsg(state,msg){
            state.msg = msg;
        }
    },
    actions: {
        handleLogin ({ commit }, {loginAccount, password}) {
            loginAccount = loginAccount.trim();
            return new Promise((resolve, reject) => {
                login({
                    loginAccount,
                    password
                }).then(res => {
                    if(!res.result){
                        commit('setMsg', res.msg);
                    }else{
                        localStorage.setItem('setToken',  res.token);
                        commit('setToken', res.token);
                    }
                    resolve();
                }).catch(err => {
                    reject(err);
                });
            });
        },
        // 退出登录
        handleLogOut ({ state, commit }) {
            return new Promise((resolve, reject) => {
                /*
                logout(state.token).then(res => {
                    commit('setToken', '');
                    commit('setAccess', []);
                    resolve();
                }).catch(err => {
                    reject(err);
                });
                */
                // 如果你的退出登录无需请求接口，则可以直接使用下面三行代码而无需使用logout调用接口
                commit('setToken', '');
                commit('setAccess', []);
                resolve();
            });
        },
        // 获取用户相关信息
        getUserInfo ({ state, commit }) {
            return new Promise((resolve, reject) => {
                getUserInfo({token:state.token}).then(res => {
                    if(res.result){
                        localStorage.setItem('setAvator',   res.user.avatorImgPath);
                        commit('setAvator', res.user.avatorImgPath);
                        localStorage.setItem('setUserName',   res.user.userName);
                        commit('setUserName',  res.user.userName);
                        localStorage.setItem('setUserId', res.user.userId);
                        commit('setUserId',  res.user.userId);
                        localStorage.setItem('setLoginAccount',  res.user.loginAccount);
                        commit('setLoginAccount',  res.user.loginAccount);
                    }
                    resolve(res);
                }).catch(err => {
                    reject(err);
                });
            });
        }
    }
};
